/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import beans.Bean;
import beans.PacienteBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Milene
 */
public class MSSQLPacienteDao extends MSSQLDao implements Dao {

    public MSSQLPacienteDao() throws Exception {
        super();
    }

    @Override
    public Bean make(ResultSet result) throws Exception {
       
        PacienteBean pb = new PacienteBean();
        pb.setAfiliado(result.getString("afiliado"));
        pb.setApellidoP(result.getString("apellido"));
        pb.setBarrio(result.getString("barrio"));
        pb.setCiudad(result.getString("ciudad"));
        pb.setDocumentoP(result.getInt("nrodocumento"));
        pb.setEmailP(result.getString("email"));
        pb.setFechaNacimientoP(result.getDate("fechanacimiento"));
        pb.setIdAfiliado(result.getInt("nroAfiliado"));
        pb.setIdPaciente(result.getInt("id_paciente"));
        pb.setNacionalidadP(result.getString("nacionalidad"));
        pb.setNombreP(result.getString("nombre"));
        pb.setObraSocial(result.getString("obrasocial"));
        pb.setSexoP(result.getString("sexo_paciente"));
        pb.setTelefonoP(result.getString("telefono"));
        pb.setTipoDoc(result.getString("tipodocumento"));
        pb.setEstadoCivil(result.getString("estado"));
        pb.setCalle(result.getString("calle"));
        pb.setNumero(result.getString("numero"));
        pb.setPiso(result.getString("piso"));
        pb.setDepto(result.getString("dpto"));
        pb.setCreacion(result.getString("creacion"));        
        return pb;
    } 

    @Override
    public List<Bean> select(Bean bean) throws Exception {
        return null;        
    }
           

    @Override
    public boolean valid(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int insert(Bean bean) throws Exception {
        long rs = -1;
         try {
            this.getConnection().setAutoCommit(false);//para mantener la integridad de la base, usar transaccion            
            this.setStatement("insertar_paciente(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            this.getStatement().setString(1,((PacienteBean) bean).getNombreP()); //
            this.getStatement().setString(2,((PacienteBean) bean).getApellidoP() );// 
            this.getStatement().setString(3,((PacienteBean) bean).getSexoP() );
            this.getStatement().setDate(4,new java.sql.Date(((PacienteBean) bean).getFechaNacimientoP().getTime()));
            this.getStatement().setString(5,((PacienteBean) bean).getEstadoCivil());
            this.getStatement().setString(6,((PacienteBean) bean).getNacionalidadP() );
            this.getStatement().setString(7,((PacienteBean) bean).getEmailP());
            this.getStatement().setString(8,((PacienteBean) bean).getTelefonoP() );
            this.getStatement().setInt(9,((PacienteBean) bean).getIdAfiliado() );
            this.getStatement().setString(10,((PacienteBean) bean).getTipoDoc() );
            this.getStatement().setInt(11,((PacienteBean) bean).getDocumentoP());
            this.getStatement().setString(12,((PacienteBean) bean).getBarrio() );
            this.getStatement().setString(13,((PacienteBean) bean).getCiudad() );
            this.getStatement().setString(14,((PacienteBean) bean).getObraSocial() );
            this.getStatement().setString(15,((PacienteBean) bean).getCalle() );
            this.getStatement().setString(16,((PacienteBean) bean).getNumero() );
            this.getStatement().setString(17,((PacienteBean) bean).getPiso() );
            this.getStatement().setString(18,((PacienteBean) bean).getDepto() );
            this.getStatement().setString(19,((PacienteBean) bean).getCreacion() );
            
            if (this.getStatement().executeUpdate()==1) {
                this.getConnection().commit();               
            }
            
            this.setStatement("get_id_paciente(?,?)");
            this.getStatement().setInt(1,((PacienteBean) bean).getIdAfiliado() );
            this.getStatement().setString(2,((PacienteBean) bean).getObraSocial() );             
            ResultSet    result = this.getStatement().executeQuery();     
              
            result.next();
            rs = result.getInt("id_paciente");         
            
        } catch (SQLException ex) {
            System.out.println("Error  " + ex);
            this.getConnection().rollback();
            rs = -2;
        } catch (Exception ex2) { 
            this.getConnection().rollback();
            rs = -3;
        }        
        finally {
            this.getConnection().setAutoCommit(true);          
            this.destroy();
        }
        return (int) rs;        
    }

    @Override
    public Bean get(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public long getSize(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int delete(Bean bean) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    
    
    
}
